﻿using System;
using System.ComponentModel.Design;
using System.IO;
using Microsoft.VisualStudio.Shell;
using PS_WSSDeploy_Package.Application;
using PS_WSSDeploy_Package.Utils;

namespace PS_WSSDeploy_Package.WdCommand
{
    class CopyToGacCmd : BaseCmd, IWdTask
    {
        public delegate int CopyAction(string path);

        public static BaseCmd Create(OleMenuCommandService service, bool enableForSandbox)
        {
            var instance = new CopyToGacCmd();
            var cmdId = new CommandID(GuidList.guidPS_WSSDeploy_PackageCmdSet, (int)PkgCmdIDList.cmdidCopyToGacCommand);
            AddCommandToMenu(service, instance, cmdId, enableForSandbox);
            return instance;
        }

        public override void DoExecute()
        {
            if (ActiveConfig.RecycleAppPoolsBeforeCopyToGac)
            {
                AddTask(new RecycleAppPoolsCmd());
            }
            AddTask(this);
        }

        public void TaskExecute()
        {
            bool gacFolderExists = Directory.Exists(GACFolderPath);
            string trgPath = ProjectTargetPath;
            bool useTrgPath = ActiveConfig.UseTargetPath;
            bool trgPathExists = File.Exists(trgPath);

            if (gacFolderExists)
            {
                string gacErrorText = string.Empty;

                int itemGac = Copy(Cc.CopyToGAC, GACFolderPath, ref gacErrorText);
                if (itemGac > 0)
                {
                    WriteOutput(string.Format(WssDeployResources.CopyToGacCmd_CopyToGacCopiedSucceeded, GACFolderPath, itemGac));
                    SetSuccess();
                }
                else
                {
                    WriteOutput(string.Format(WssDeployResources.CopyToGacCmd_CopyToGacError, GACFolderPath, gacErrorText));
                    SetFailed();
                }
            }
            else if (!useTrgPath)
            {
                WriteOutput(string.Format(WssDeployResources.CopyToGacCmd_CopyToGacError, GACFolderPath, "folder not found"));
                SetFailed();
            }

            if (useTrgPath)
            {
                if (trgPathExists)
                {
                    string trgErrorText = string.Empty;

                    int itemTrg = Copy(Cc.CopyTargetToGAC, trgPath, ref trgErrorText);
                    if (itemTrg > 0)
                    {
                        WriteOutput(string.Format(WssDeployResources.CopyToGacCmd_CopyToGacSucceeded, trgPath));
                        SetSuccess();
                    }
                    else
                    {
                        WriteOutput(string.Format(WssDeployResources.CopyToGacCmd_CopyToGacError, trgPath, trgErrorText));
                        SetFailed();
                    }
                }
                else
                {
                    WriteOutput(string.Format(WssDeployResources.CopyToGacCmd_CopyToGacError, trgPath, "file not found"));
                    SetFailed();
                }
            }
        }

        private static int Copy(CopyAction action, string path, ref string errorText)
        {
            int itemCount = 0;
            try
            {
                itemCount = action(path);
            }
            catch (Exception ex)
            {
                errorText = ex.Message;
            }
            return itemCount;
        }
    }
}
